package com.example.mykotlin.service

import com.example.mykotlin.model.entity.RolePermission
import com.example.mykotlin.model.entity.UserPermission
import com.example.mykotlin.model.entity.UserRole
import com.example.mykotlin.model.enums.SystemRole
import com.mybatisflex.kotlin.extensions.db.deleteWith
import com.mybatisflex.kotlin.extensions.db.insert
import com.mybatisflex.kotlin.extensions.kproperty.eq
import org.springframework.scheduling.annotation.Async
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional


@Service
class AsyncEventService {
    @Async
    @Transactional(rollbackFor = [Exception::class])
    fun deleteUserEvent(userId: Long) {
        deleteWith<UserRole> { UserRole::userId eq userId }
        deleteWith<UserPermission> { UserPermission::userId eq userId }
    }

    @Async
    fun deleteRoleEvent(roleId: String) {
        deleteWith<RolePermission> { RolePermission::roleId eq roleId }
    }

    @Async
    fun insertUserEvent(userId: Long) {
        insert(UserRole(userId, SystemRole.USER.name))
    }

}